package advertising.handler

import advertising.entity._
import org.apache.commons.lang3.StringUtils
import org.apache.spark.sql.Row

import scala.collection.mutable
import scala.collection.mutable.{ArrayBuffer}

//  .--,       .--,
// ( (  \.---./  ) )
//  '.__/o   o\__.'
//     {=  ^  =}
//      >  -  <
//     /       \
//    //       \\
//   //|   .   |\\
//   "'\       /'"_.-~^`'-.
//      \  _  /--'         `
//    ___)( )(___
//   (((__) (__)))    高山仰止, 景行行止; 虽不能至, 心向往之.
//                    High mountains, scenery. Although not to, the heart yearns for it. 
object TypeCastHandler {

  def arr2AdRawInfo(arr:Array[String]):RawTabData = {
    // 增强类型转换
    import advertising.boost.SuperNumber.string2SuperNumber
    RawTabData(
      arr(0),
      arr(1).toIntOrElse0(),
      arr(2).toIntOrElse0(),
      arr(3).toIntOrElse0(),
      arr(4).toIntOrElse0(),
      arr(5),
      arr(6),
      arr(7).toIntOrElse0(),
      arr(8).toIntOrElse0(),
      arr(9).toDoubleOrElse0(),
      arr(10).toDoubleOrElse0(),
      arr(11),
      arr(12),
      arr(13),
      arr(14),
      arr(15),
      arr(16),
      arr(17).toIntOrElse0(),
      arr(18),
      arr(19),
      arr(20).toIntOrElse0(),
      arr(21).toIntOrElse0(),
      arr(22).toDoubleOrElse0(),
      arr(23).toDoubleOrElse0(),
      arr(24),
      arr(25),
      arr(26).toIntOrElse0(),
      arr(27),
      arr(28).toIntOrElse0(),
      arr(29),
      arr(30).toIntOrElse0(),
      arr(31).toIntOrElse0(),
      arr(32).toIntOrElse0(),
      arr(33),
      arr(34).toIntOrElse0(),
      arr(35).toIntOrElse0(),
      arr(36).toIntOrElse0(),
      arr(37),
      arr(38).toIntOrElse0(),
      arr(39).toIntOrElse0(),
      arr(40).toDoubleOrElse0(),
      arr(41).toDoubleOrElse0(),
      arr(42).toIntOrElse0(),
      arr(43),
      arr(44).toDoubleOrElse0(),
      arr(45).toDoubleOrElse0(),
      arr(46),
      arr(47),
      arr(48),
      arr(49),
      arr(50),
      arr(51),
      arr(52),
      arr(53),
      arr(54),
      arr(55),
      arr(56),
      arr(57).toIntOrElse0(),
      arr(58).toDoubleOrElse0(),
      arr(59).toIntOrElse0(),
      arr(60).toIntOrElse0(),
      arr(61),
      arr(62),
      arr(63),
      arr(64),
      arr(65),
      arr(66),
      arr(67),
      arr(68),
      arr(69),
      arr(70),
      arr(71),
      arr(72),
      arr(73).toIntOrElse0(),
      arr(74).toDoubleOrElse0(),
      arr(75).toDoubleOrElse0(),
      arr(76).toDoubleOrElse0(),
      arr(77).toDoubleOrElse0(),
      arr(78).toDoubleOrElse0(),
      arr(79),
      arr(80),
      arr(81),
      arr(82),
      arr(83),
      arr(84).toIntOrElse0()
    )
  }

  def row2BaseTabData(row:Row):BaseTabData = {
    val provinceName = FieldValueHandler.getProvinceName(row) // 省名称
    val cityName = FieldValueHandler.getCityName(row) // 市名称
    val ispName = FieldValueHandler.getIspName(row) // 运营商名称
    val networkMannerName = FieldValueHandler.getNetworkName(row) // 网络名称
    val deviceName = FieldValueHandler.getDeviceName(row) // 设备名称
    val osName = FieldValueHandler.getOSName(row) // 系统名称
    val appName = FieldValueHandler.getAppName(row) // app 名称
    val callbackDate = FieldValueHandler.getCallbackDate(row) // 获取回调时间
    val markDate = DateHandler.getYear2Day(callbackDate)

    val requestMode = FieldValueHandler.getRequestMode(row, -1)
    val processNode = FieldValueHandler.getProcessNode(row, -1)
    val isEffective = FieldValueHandler.getIsEffective(row, -1)
    val isBilling = FieldValueHandler.getIsBilling(row, -1)
    val isBid = FieldValueHandler.getIsBid(row, -1)
    val adOrderId = FieldValueHandler.getAdOrderId(row, -1)
    val isWin = FieldValueHandler.getIsWin(row, -1)
    val winPrice = FieldValueHandler.getWinPrice(row, 0.0)
    val adPayment = FieldValueHandler.getAdPayment(row, 0.0)

    var requestNum: Long = 0
    var validRequestNum: Long = 0
    var adRequestNum:Long = 0
    var biddingNum: Long = 0
    var succeedBiddingNum: Long = 0
    var showedNum: Long = 0
    var clickedNum: Long = 0
    var adCost: Double = 0.0
    var adConsume: Double = 0.0

    // 原始请求
    if(requestMode == 1 && processNode >= 1){
      requestNum = 1
    }
    // 成功请求
    if(requestMode == 1 && processNode >= 2){
      validRequestNum = 1
    }
    // 广告请求
    if(requestMode == 1 && processNode == 3){
      adRequestNum = 1
    }
    // 参与竞价数
    if(isEffective == 1 && isBilling ==1 && isBid == 1 && adOrderId == 1){
      biddingNum = 1
    }
    // 竞价成功数
    if(isEffective == 1 && isBilling == 1 && isWin == 1){
      succeedBiddingNum = 1
    }
    // 展示数
    if(requestMode == 2 && isEffective == 1){
      showedNum = 1
    }
    // 点击数
    if(requestMode == 3 && isEffective == 1){
      clickedNum = 1
    }
    // DSP广告消费
    if(isEffective == 1 && isBilling == 1 && isWin == 1){
      adCost = winPrice / 1000
      adConsume = adPayment / 1000
    }

    BaseTabData(
      provinceName,
      cityName,
      ispName,
      networkMannerName,
      deviceName,
      osName,
      appName,
      requestNum,
      validRequestNum,
      adRequestNum,
      biddingNum,
      succeedBiddingNum,
      showedNum,
      clickedNum,
      adCost,
      adConsume,
      markDate
    )
  }

}
